# coding=utf-8

import time


def get_date(days):
    import datetime
    month_days_dict = {1: 31, 3: 31, 5: 31, 7: 31, 8: 31, 10: 31, 12: 31, 2: 30, 4: 30, 6: 30, 9: 30, 11: 30}
    today_date = datetime.date.today()
    str_time = today_date.strftime("%Y-%m-%d")
    new_today = str_time.split('-')
    year = new_today[0]
    month = new_today[1]
    day = new_today[2]
    if int(days) >= 0:
        maxday = month_days_dict.get(int(month))
        n_day = int(days) + int(day)
        if n_day <= maxday:
            n_date = year + '-' + month + '-' + str(n_day)
            new_date = datetime.datetime.strptime(n_date, '%Y-%m-%d').date()
            return f"当前日期为:{new_date}"
        else:
            num = n_day - maxday
            if num < 10:
                p_month = int(month) + 1
            else:
                str_num = str(num)[0]
                p_month = int(month) + int(str_num)
            n_date = year + '-' + str(p_month) + '-' + str(num)
            new_date = datetime.datetime.strptime(n_date, '%Y-%m-%d').date()
            return f"当前日期为:{new_date}"
    else:
        n_day = int(days) + int(day)
        if n_day > 0:
            n_date = year + '-' + month + '-' + str(n_day)
            new_date = datetime.datetime.strptime(n_date, '%Y-%m-%d').date()
            return f"当前日期为:{new_date}"
        elif n_day == 0:
            p_month = int(month) - 1
            p_days = month_days_dict.get(int(p_month))
            l_date = year + '-' + str(p_month) + '-' + str(p_days)
            new_date = datetime.datetime.strptime(l_date, '%Y-%m-%d').date()
            return f"当前日期为:{new_date}"
        else:
            p_month = int(month) - 1
            d_days = month_days_dict.get(int(p_month))
            c_day = d_days + n_day
            l_date = year + '-' + str(p_month) + '-' + str(c_day)
            new_date = datetime.datetime.strptime(l_date, '%Y-%m-%d').date()
            return f"当前日期为:{new_date}"


day = input('请输入需要查询的天数：正数代表几天前，负数代表前几天:')
print(get_date(int(day)))


# 获取当前时间
print(
    time.localtime())  # 精度到秒 time.struct_time(tm_year=2020, tm_mon=11, tm_mday=6, tm_hour=16, tm_min=1, tm_sec=17, tm_wday=4, tm_yday=311, tm_isdst=0)

# 时间戳
print(time.time())  # 1604649677.3256476

# time.struct_time -> str
print(time.strftime("%Y-%m-%d %H:%M:%S"))  # 2020-11-06 16:01:17

# str -> time.struct_time
print(time.strptime("2020-11-06 15:50:36",
                    "%Y-%m-%d %H:%M:%S"))  # time.struct_time(tm_year=2020, tm_mon=11, tm_mday=5, tm_hour=21, tm_min=50, tm_sec=36, tm_wday=3, tm_yday=310, tm_isdst=-1)

# time.struct_time -> tuple
print(tuple(time.struct_time(time.localtime())))  # (2020, 11, 6, 16, 1, 17, 4, 311, 0)

# tuple -> time.struct_time
a = (2020, 11, 6, 15, 25, 31, 4, 311, 0)
print(time.struct_time(
    a))  # time.struct_time(tm_year=2020, tm_mon=11, tm_mday=6, tm_hour=15, tm_min=25, tm_sec=31, tm_wday=4, tm_yday=311, tm_isdst=0)

# timestamp -> time.struct_time
print(time.mktime(time.localtime()))  # 1604649677.0

# datetime
from datetime import datetime

print(datetime.today())  # 2020-11-06 16:01:17.328648
print(datetime.now(tz=None))  # 可以指定时区 2020-11-06 16:01:17.328647
print(datetime.utcnow())  # 2020-11-06 08:01:17.328647

# datetime.datetime -> str
print(datetime.strptime('2020-11-6 15:19:59', '%Y-%m-%d %H:%M:%S'))  # 2020-11-06 18:19:59

# datetime.datetime -> timetuple -> tuple
tt = datetime(2020, 11, 6, 15, 50, 40).timetuple()
print(tuple(tt))  # (2020, 11, 6, 15, 50, 40, 4, 311, -1)

# tuple -> datetime.datetime
print(datetime(2020, 11, 6, 16, 00, 00))  # 2020-11-06 16:00:00

# datetime.datetime -> timestamp
print(datetime(2020, 11, 6, 16, 00, 00).timestamp())  # 1604649600.0

print(datetime.fromtimestamp(1604649600.0))  # 2020-11-06 16:00:00
